import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/entity/count_entity.dart';
import 'package:shopping_group/modle/person/bloc/bind_phone_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:shopping_group/view/button_progress_view.dart';

class BindPhonePage extends StatefulWidget {
  @override
  _TaskBlockInfoViewState createState() => _TaskBlockInfoViewState();
}

class _TaskBlockInfoViewState extends State<BindPhonePage>
    with TickerProviderStateMixin {
  TextEditingController _controllerPhone = TextEditingController();
  TextEditingController _controllerCode = TextEditingController();
  bool isSecrecy = true;
  BindPhoneBloc _bloc;
  // SelectiontEntity _selection = SelectiontEntity();
  CountEntity _countEntity = CountEntity();

  @override
  void initState() {
    super.initState();
    _bloc = BindPhoneBloc();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '绑定手机号'),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          height: double.infinity,
          decoration: BoxDecoration(
            color: Colors.white,
          ),
          child: Column(
            children: <Widget>[
              SizedBox(
                height: MediaQuery.of(context).size.height / 10,
              ),

              SizedBox(
                height: 4,
              ),

              // WidgetUtils.getTextColorView(
              //     '土猫到家员工端', Colours.text_2222, Dimens.FONT_SIZE_18,
              //     fontweight: FontWeight.bold),
              SizedBox(
                height: 16,
              ),
              Column(
                children: <Widget>[
                  _loginBody(),
                  //  _bottomWidget(),
                  SizedBox(
                    height: 42,
                  ),
                ],
              ),
              // flex: 3,
            ],
          ),
        ),
      ),
    );
  }

  Widget _loginBody() {
    return Container(
      //margin: const EdgeInsets.only(left: 6, right: 6),
      child: Column(
        children: <Widget>[
          Padding(
              padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
              child: Row(
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                    '+86',
                    Colours.text_2222,
                    Dimens.FONT_SIZE_18,
                  ),
                  SizedBox(
                    width: 16,
                  ),
                  WidgetUtils.drawVerticalContainer(
                      height: 24, color: Colours.transparent_8),
                  Expanded(
                      child: WidgetUtils.getTextFieldView(
                          _controllerPhone, '请输入手机号',
                          width: MediaQuery.of(context).size.width,
                          hintFontSize: Dimens.FONT_SIZE_18)),
                ],
              )),
          WidgetUtils.drawDivider(
            color: Colours.transparent_8,
            height: 0.5,
          ),
          isDxOrPasswordView(),
          WidgetUtils.drawDivider(
            color: Colours.transparent_8,
            height: 0.5,
          ),
          SizedBox(
            height: 50,
          ),
          ButtonProgressView.crateCupertinoView('确定绑定手机号',
              horizontalPadding: 16,
              textColor: Colours.title_white,
              blackColor: Colours.themeColor, cupertinoCall: () {
            FocusScope.of(context).requestFocus(FocusNode());
            String phone = _controllerPhone.text;
            String code = _controllerCode.text;
            if (phone.isEmpty) {
              BotToast.showText(text: '请输入手机号');
              return;
            }

            if (phone.length < 5) {
              BotToast.showText(text: '用户名至少11位');
              return;
            }

            if (ObjectUtil.isEmptyString(code)) {
              BotToast.showText(text: '请输入短信验证码');
              return;
            }
            Map<String, dynamic> data = new Map<String, dynamic>();
            data['openId'] = BaseConstant.weixinOperId;
            data['phone'] = phone;
            data['validateCode'] = _controllerCode.text;
            _bloc.bindDate.add(data);
          }),
        ],
      ),
    );
  }

  Widget isDxOrPasswordView() {
    return Column(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            // Expanded(
            //   flex: 1,
            //   child: Container(
            //     padding: const EdgeInsets.only(left: 16),
            //     alignment: FractionalOffset.centerLeft,
            //     child: WidgetUtils.getTextColorView(
            //         '验证码：', Colours.title_white, Dimens.FONT_SIZE_16),
            //   ),
            // ),
            Expanded(
              child: StreamBuilder(
                stream: _bloc.pushDate,
                initialData: _countEntity,
                builder:
                    (BuildContext context, AsyncSnapshot<Object> snapshot) {
                  Object object = snapshot.data;
                  if (object is CountEntity) {
                    _countEntity = object;
                  }
                  return Padding(
                    padding:
                        const EdgeInsets.only(left: 16, top: 12, bottom: 12),
                    child: WidgetUtils.buildTextFieldCode(
                        _controllerCode,
                        TextInputType.phone,
                        '请输入验证码',
                        List<TextInputFormatter>()
                          ..add(LengthLimitingTextInputFormatter(18)),
                        _countEntity.codeCountdownStr,
                        _countEntity.colorStr,
                        colorBox: _countEntity.colorBox,
                        colorBorder: _countEntity.colorBorder,
                        hitColor: Colours.text_bd,
                        textColor: Colours.text_2222, codeCallback: (callback) {
                      _usercode();
                    }),
                  );
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  //校验获取验证码格式
  void _usercode() {
    String phonename = _controllerPhone.text;
    if (ObjectUtil.isEmptyString(phonename)) {
      BotToast.showText(text: '请输入您的手机号');
      return;
    }
    if (phonename.length != 11) {
      BotToast.showText(text: '请输入正确的手机号');
      return;
    }
    _bloc.verificationDate.add(phonename);
  }

  @override
  void dispose() {
    _controllerPhone.dispose();
    _bloc.dispose();
    super.dispose();
  }
}
